<include file="public@header" />
<style>
th{
	font-size:14px;
}
td input{ width:auto; max-width:100px;}
</style>
</head>
<body>
	<div class="wrap js-check-wrap">
		<ul class="nav nav-tabs">
        	<li><a href="{:url('RockOrder/index')}">石材下单列表</a></li>
			<li class="active"><a href="{:url('RockOrder/orderinfo')}">{:lang('添加采购订单')}</a></li>
		</ul>
        <form  class="form-horizontal js-ajax-form" method="post" action="{:url('RockOrder/orderinfo')}" enctype="multipart/form-data">
        	<fieldset>
                <div style="width:100%;">
                    <div class="p_title">
                        <span class="span_title">采购信息</span>
                    </div>
                    <div class="col-md-12"  style="border:1px solid #e2e2e2; padding-top:20px;">
                        <div class="form-group">
                            <label class="control-label">合同编号</label>
                            <div class="col-md-2">
                                <input type="text" class="form-control" name="pact[pact_num]" value="{$results.pact_num|default=''}" readonly>
                            </div>
                            <label class="control-label">合同名称</label>
                            <div class="col-md-2">
                                <input type="text" class="form-control" name="pact[pact_name]" value="" required>
                            </div>
                            <label class="control-label">供货商名称</label>
                            <div class="col-md-2">
                                <select name="pact[supplier]"   class="form-control">
                                    <foreach name="userlist" item="vou">
                                        <if condition=" isset($result['supplier'])">
                                            <option value="{$vou.id}" <if condition=" $result['supplier'] eq $vou.id"> selected </if>>{$vou.department_name}-{$vou.user_nickname}</option>
                                            <else/>
                                            <option value="{$vou.id}">{$vou.department_name}-{$vou.user_nickname}</option>
                                        </if>
                                    </foreach>
                                </select>
                            </div>
                            <label class="control-label">下单时间</label>
                            <div class="col-md-2">
                                <input type="text" class="form-control js-date" name="pact[add_time]" value="{$results.add_time|default=''}" readonly>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label">预期交付时间</label>
                            <div class="col-md-2">
                                <input type="text" class="form-control js-date" name="pact[due_time]" value="{$results.due_time|default=''}" autocomplete="off" required >
                            </div>
                            <label class="control-label">交货地点</label>
                            <div class="col-md-2">
                                <input type="text" class="form-control" name="pact[trading_address]" value="" required>
                            </div>
                            <label class="control-label">订单总价</label>
                            <div class="col-md-2" style="width: 21%">
                                <input type="text" id="total_price" class="form-control total_price" name="pact[total_price]" value="" required>
                                <input type="checkbox" id="include" name="pact[include]" checked> 含运费
                                <input type="checkbox" id="tax" name="pact[tax]" checked> 含税金
                            </div>
                            <label class="control-label" style="width: 62px">到付运费</label>
                            <div class="col-md-2">
                                <input type="text" class="form-control delivery_fee" id="payfor" name="pact[delivery_fee]" value=""  disabled>
                            </div>
                        </div>
                            <div class="form-group">
                                <label class="control-label">税金</label>
                                <div class="col-md-2">
                                    <input type="text" class="form-control tax_price" id="tax_price" name="pact[tax_price]" value=""  disabled>
                                </div>
                                <label class="control-label">下单人</label>
                                <div class="col-md-2">
                                    <input type="text" class="form-control" name="pact[add_user]" value="{:cmf_get_admin_name()}" disabled>
                                </div>
                                <label class="control-label">订单备注</label>
                                <div class="col-md-3">
                                    <textarea rows="1" class="form-control" name="pact[remark]" placeholder="" style="width:100%"></textarea>
                                </div>
                            </div>
                    </div>
                    <div class="p_title">
                        <span class="span_title">订单详情</span>
                    </div>
                    <div class="col-md-12"  style="border:1px solid #e2e2e2; padding-top:20px;overflow:hidden;overflow-x:auto;width:100%">
                        <table class="table table-hover table-bordered" style="max-width: 100% !important;width: 100% !important;">
                            <thead>
                            <tr>
                                <th width="8%">ID  <div class="btn btn-success" id="check-pro">选择产品</div></th>
                                <th width="12%">{:lang('产品名称')}</th>
                                <th width="10%">{:lang('规格')}</th>
                                <th  width="6%">{:lang('单位')}</th>
                                <th  width="8%">{:lang('数量')}</th>
                                <th width="10%">{:lang('成本单价')}</th>
                                <th width="10%">{:lang('合计价格')}</th>
                                <th width="5%">{:lang('BOM')}</th>
                                <th width="15%">{:lang('应用位置')}</th>
                                <th width="15%">{:lang('备注')}</th>
                            </tr>
                            </thead>
                            <tbody id="pro_tr" >
                            </tbody>
                        </table>
                        <div class="pagination">{$page}</div>
                    </div>
                </div>
            </fieldset>
            <div class="form-actions">
                <button type="submit" class="btn btn-primary js-ajax-submit">{:lang('ADD')}</button>
                <a class="btn btn-default" href="javascript:history.back(-1);">{:lang('BACK')}</a>
            </div>
        </form>
	</div>
	<script type="text/javascript" src="__STATIC__/js/admin.js?v={:time()}"></script>
    <script>
        $('#check-pro').click(function () {
            openIframeLayer("{:url('ajaxProduct')}?type=0", '请选择产品', {
                area: ['90%', '85%'],
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    var iframeWin          = window[layero.find('iframe')[0]['name']];
                    var selectedProduct = iframeWin.confirm();
                    if (selectedProduct.productName.length == 0) {
                        layer.msg('请选择产品');
                        return;
                    }
                    $.each(selectedProduct.productList, function (i, n) {
                        if ( $('#pro_tr').find('tr').hasClass('pro_id_'+n.pro_id)){
                            var  class_pro = $('#pro_tr').find('.pro_id_'+n.pro_id);
                            class_pro.find('.num').val(parseInt(class_pro.find('.num').val())+1);
                        }else {
                           var ii = n.pro_id;
                            var  html_tr ='<tr class="pro_id_'+n.pro_id +'">\n' +
                                '   <td>\n' +
                                '    <input type="hidden" class="form-control pro_id" name="product['+ii+'][pro_id]" value="'+n.pro_id+'">\n' +n.pro_id +
                                '    </td>\n' +
                                '    <td>'+n.pro_name+'</td>\n' +
                                '    <td>'+n.pro_length+'mm*'+n.pro_width +'mm *'+n.pro_height+'mm</td>\n' +
                                '    <td>套' +
                                '<input type="hidden" class="form-control" name="product['+ii+'][unit]" value="套" ></td>\n' +
                                '    <td><input type="text" class="form-control num" name="product['+ii+'][total_num]" value="1"></td>\n' +
                                '    <td><input type="text" class="form-control cost_unit_price " name="product['+ii+'][cost_unit_price]" value=""></td>\n' +
                                '    <td><input type="text" class="form-control total_price" name="product['+ii+'][total_price]" value=""></td>\n' +
                                '    <td><a href="javascript:;" data-pro_id="'+n.pro_id+'" class="product_lock">查看</a></td>\n' +
                                '    <td><input type="text" class="form-control" style="max-width: 350px;" name="product['+ii+'][application_location]" value=""></td>\n' +
                                '    <td><input type="text" class="form-control" style="max-width: 350px;" name="product['+ii+'][remark]" value=""></td>\n' +
                                '</tr>';
                            $('#pro_tr').append(html_tr);
                        }
                    })

                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            });
        });


        $(document).on("blur",'.cost_unit_price',function (event) {
            var k = event.target.offsetLeft,target  = event.target,total=0,
                className = $(target).parent().parent()[0].className;
            var num = $('.'+className).find(".num").val(),
                unit_price =event.target.value;
            $('.'+className).find('.total_price').val(num * unit_price);

            $("#pro_tr tr").each(function() {
                var total_price = $(this).find('.total_price').val();
                var v1 = parseFloat(total_price);
                v1 = isNaN(v1)?0:v1;
                total += v1;
            });
            $("#total_price").val(total);//总计

        })
        $(document).on("blur",'.num',function (event) {
            var k = event.target.offsetLeft,target  = event.target,total=0,
                className = $(target).parent().parent()[0].className;;
            var unit_price = $('.'+className).find(".cost_unit_price").val(),
                num =event.target.value;
            $('.'+className).find('.total_price').val(num * unit_price);
            $("#pro_tr tr").each(function() {
                var total_price = $(this).find('.total_price').val();
                var v1 = parseFloat(total_price);
                v1 = isNaN(v1)?0:v1;
                total += v1;
            });
            $("#total_price").val(total);//总计
        })
        $(document).on("click",'.product_lock',function (event) {
            var pro_id = $(this).data('pro_id');
            var href = $(this).data('href'),
                title = $(this).data('title');
            openIframeLayer("{:url('ajaxProduct')}?type=2&pro_id="+pro_id, '产品详细资料', {
                area: ['95%', '80%'],
                // btn: ['确定', '取消'],
                yes: function (index, layero) {
                    // var iframeWin = window[layero.find('iframe')[0]['name']];
                    // var selectedProduct = iframeWin.confirm();
                    // if (selectedProduct.productName.length == 0) {
                    //     layer.msg('请选择产品');
                    //     return;
                    // }
                }
            })
        })


        $("#include").change(function(){
            if($("#include").is(":checked")){
                $("#payfor").attr('disabled','disabled');
            }else{
                $("#payfor").removeAttr('disabled');
            }
        })

        $("#tax").change(function(){
            if($("#tax").is(":checked")){
                $("#tax_price").attr('disabled','disabled');
            }else{
                $("#tax_price").removeAttr('disabled');
            }
        })
        function openLayer(url, title, options) {
            var params = {
                type: 1,
                maxmin : true,
                title: title,
                // skin: 'layui-layer-nobg',
                anim: -1,
                shade: [0.001, '#000000'],
                shadeClose: true,
                area: ['98%', '90%'],
                move: title,
                content: url,
                yes: function (index, layero) {
                    //do something
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            };
            params     = options ? $.extend(params, options) : params;
            Wind.css('layer');
            Wind.use("layer", function () {
                layer.open(params);
            });
        }

    </script>
</body>
</html>